﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.Restaurant;
using PetaPoco;

namespace BrnMall.Manager.Manager.Restaurant
{
    public static class LineManager
    {
        public static void AddLineItem(LineItem item)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(item);
            }
        }

        public static void EditLineItem(LineItem item)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(item);
            }
        }

        public static LineItem GetLineItem(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<LineItem>(id);
            }
        }

        public static void DeleteLineItem(int id)
        {
            using (var db = new QDMallDatabase())
            {
                try
                {
                    db.BeginTransaction();
                    db.Execute("delete bma_lineitem where id=@0", id);
                    db.Execute("delete bma_linequeue where LineId=@0", id);
                    db.CompleteTransaction();
                }
                catch
                {
                    db.AbortTransaction();
                    throw;
                }

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public static IList<LineItem> GetStoreLineItems(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<LineItem>("select * from bma_lineitem where storeid=@0 order by displayorder ", storeId);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="queue"></param>
        public static void AddLineQueue(LineQueue queue)
        {
            using (var db = new QDMallDatabase())
            {
                try
                {
                    db.BeginTransaction();
                    db.Insert(queue);
                    db.Execute($"update bma_lineitem set NextNumber={queue.LineNumber + 1} where Id={queue.LineId}");
                    db.CompleteTransaction();
                }
                catch
                {
                    db.AbortTransaction();
                    throw;
                }

            }
        }

        public static void EditLineQueue(LineQueue queue)
        {
            using (var db = new QDMallDatabase())
            {
                db.Update(queue);
            }
        }

        public static void DeleteLineQueue(long id)
        {
            using (var db = new QDMallDatabase())
            {
                db.Delete<LineQueue>(id);
            }
        }
        public static LineQueue GetLineQueueByUid(int storeId, int uid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<LineQueue>("select * from bma_linequeue where storeid=@0 and uid=@1 and queuestate=0", storeId, uid);
            }
        }
        public static LineQueue GetLineQueue(long id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<LineQueue>(id);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lineId"></param>
        /// <param name="storeid"></param>
        /// <param name="queueState"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageNumber"></param>
        /// <returns></returns>
        public static Page<LineQueue> GetLineQueuePage(int lineId, int storeid, int queueState, long pageSize = 15, long pageNumber = 1)
        {
            using (var db = new QDMallDatabase())
            {
                var sql = new StringBuilder();
                sql.Append($"select * from bma_linequeue where storeid={storeid} ");
                if (lineId != 0)
                    sql.Append($" AND lineid={lineId}");
                if (queueState != -1)
                    sql.Append($" AND queuestate={queueState}");
                return db.Page<LineQueue>(pageNumber, pageSize, sql.ToString());
            }
        }

        /// <summary>
        /// 统计当前队列人数
        /// </summary>
        /// <param name="id">当前队列排序ID</param>
        /// <param name="lineid">队列ID</param>
        /// <param name="queueState">状态0排队中，1正叫号，2已就餐，3已过号</param>
        /// <returns></returns>
        public static int GetLineQueueCount(long id, int lineid, int queueState)
        {
            using (var db = new QDMallDatabase())
            {
                var sql = new StringBuilder();
                sql.Append("select count(*) from bma_linequeue where 1=1");
                if (lineid > 0)
                    sql.Append($" and lineid={lineid}");

                sql.Append($" and queueState={queueState}");
                if (id > 0)
                    sql.Append($"  and id<{id}");

                return db.ExecuteScalar<int>(sql.ToString());

            }
        }

        public static void DeleteLineQueue(int storeid, int uid)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("delete from bma_linequeue where storeId=@0 and uid=@1", storeid, uid);
            }
        }
        /// <summary>
        /// 判断当前用户是否在排队队列中
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static bool CheckInLineQueue(int storeId, int uid)
        {
            using (var db = new QDMallDatabase())
            {
                var count = db.ExecuteScalar<int>(
                    "select count(*) from bma_linequeue where storeId=@0 and uid=@1 and queueState=0", storeId, uid);
                return count > 0;
            }
        }
        /// <summary>
        /// 重置所有排队状态
        /// </summary>
        public static void ResetLineQueue()
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("truncate table bma_linequeue");
                db.Execute("update bma_lineitem set CurrNumber=0,NextNumber=1");
            }
        }
        /// <summary>
        /// 更新队列的状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="queueState">1正叫号，2已就餐，3已过号</param>
        public static void UpdateLineQueueState(long id, int queueState)
        {
            var lineQueue = GetLineQueue(id);
            if (lineQueue == null) return;
            using (var db = new QDMallDatabase())
            {
                switch (queueState)
                {
                    case 1:
                        db.Execute($"update bma_linequeue set queuestate=1 where id={id}");
                        db.Execute(
                            $"update bma_lineitem set CurrNumber={lineQueue.LineNumber} where id={lineQueue.LineId}");
                        break;
                    case 2:
                    case 3:
                        db.Execute($"update bma_linequeue set queuestate={queueState} where id={id}");
                        break;

                }
            }
        }
        
    }
}
